package util

import (
	"fmt"
	"io/ioutil"
	"os"
)

func CreateDir(path string) error {
	_, err := os.Stat(path)
	if err == nil {
		return nil
	}
	if os.IsNotExist(err) {
		// 创建文件夹
		err := os.MkdirAll(path, os.ModePerm)
		if err != nil {
			//fmt.Printf("mkdir failed![%v]\n", err)
			return fmt.Errorf("mkdir faild")
		} else {
			return nil
		}
	}
	return err
}

func GetAllFile(pathname, prefix string) ([]map[string]interface{}, error) {
	var s []map[string]interface{}
	s = make([]map[string]interface{}, 0)
	rd, err := ioutil.ReadDir(pathname)
	if err != nil {
		fmt.Println("read dir fail:", err)
		return s, err
	}

	for _, fi := range rd {
		if !fi.IsDir() {
			//fullName := pathname + "/" + fi.Name()
			//s = append(s, fullName)
			s = append(s, map[string]interface{}{
				"src": fmt.Sprintf("%s/%s", prefix, fi.Name()),
				"mod": fi.ModTime().Unix()*1000,
			})
		}
	}
	return s, nil
}